//
//  ViewController.m
//  jPush
//
//  Created by shaneZhang on 15/5/25.
//  Copyright (c) 2015年 shaneZhang. All rights reserved.
//

#import "ViewController.h"
#import "Base64.h"

@interface ViewController()
@property (weak) IBOutlet NSTextField *titleTextFields;
@property (weak) IBOutlet NSTextField *njTextFields;
@property (weak) IBOutlet NSButton *pushButton;
@property (weak) IBOutlet NSTextField *resultTextFields;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view.
//    [self sendMessageToJPush];
    // 添加响应事件
    [self.pushButton setAction:@selector(sendButtonClick)];
}

- (void)setRepresentedObject:(id)representedObject
{
    [super setRepresentedObject:representedObject];

}


- (void)sendButtonClick
{
    if (![self checkDataComplete])
    {
        return;
    }
    
    // 发送消息
    [self sendMessageToJPush];
}

- (BOOL)checkDataComplete
{
    NSString *title = self.titleTextFields.stringValue;
    if (title == nil || [title isKindOfClass:[NSNull class]] || title.length <= 0)
    {
        return NO;
    }
    
    title = self.njTextFields.stringValue;
    if (title == nil || [title isKindOfClass:[NSNull class]] || title.length <= 0)
    {
        return NO;
    }
    return YES;
}

- (void)sendMessageToJPush
{

    NSDictionary *params = [self getParamas];
    NSString *headerString = [self getHttpHeaderString];

    //1.准备网址
//    NSURL *url = [NSURL URLWithString:@"https://api.jpush.cn/v3/push"];
        NSURL *url = [NSURL URLWithString:@"https://api.jpush.cn/v3/push/validate"];
    //2.准备请求对象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //2.1设置请求方式
    [request setHTTPMethod:@"POST"];
    //2.2设置请求参数
    NSData *param = [[self dictionaryToJson:params] dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:param];
    NSString *h = [NSString stringWithFormat:@"Basic %@",headerString];
    
    NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
    [dictM setObject:@"application/json" forKey:@"Content-Type"];
    [dictM setObject:h forKey:@"Authorization"];
    [request setAllHTTPHeaderFields:dictM];
    //3.创建链接对象,并发送请求,获取结果
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    //4.解析
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    //5.显示
    NSLog(@"%@",dict);
    NSString *result = [self dictionaryToJson:dict];
    self.resultTextFields.stringValue = result;
    
}

- (NSString*)dictionaryToJson:(NSDictionary *)dic
{
    NSError *parseError = nil;
    NSData  *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}


- (NSString *)getHttpHeaderString
{
   NSString *appKey = @"";
   NSString *secrerts = @"";
   NSString *all = [NSString stringWithFormat:@"%@:%@",appKey,secrerts];
    NSString *base64String = [Base64 base64StringBystring:all];
    
    return base64String;
}

- (NSDictionary *)getParamas
{
    NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
    
    // 全部平台
    [dictM setObject:@"all" forKey:@"platform"];
    // 全部设备
    [dictM setObject:@"all" forKey:@"audience"];
    // 通知消息
    [dictM setObject:[self getNotification] forKey:@"notification"];
    
    return dictM;
}


- (NSDictionary *)getNotification
{
    NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
    
    NSString *message = [NSString stringWithFormat:@"今天小编给大家带来的节目是%@的《%@》,时光电台记忆中的声音感谢您的用心聆听!",self.njTextFields.stringValue,self.titleTextFields.stringValue];
    
    [dictM setObject:message forKey:@"alert"];
    [dictM setObject:[self getIOSNotification] forKey:@"ios"];
    
    return dictM;
}


- (NSDictionary *)getIOSNotification
{
    NSMutableDictionary *dictM  = [NSMutableDictionary dictionary];
    [dictM setValue:@"sound.caf" forKey:@"sound"];
    [dictM setValue:@(1) forKey:@"badge"];
    [dictM setValue:[NSNumber numberWithBool:true] forKey:@"content-available"];
    return dictM;
}
@end
